package com.aijk.web.admin.service;

import com.aijk.commons.utils.Collections3;
import com.aijk.commons.web.constant.DubboVersionSystemConstant;


import com.aijk.ehm.sys.api.ModuleApi;
import com.aijk.ehm.sys.api.SysAdminRoleApi;
import com.aijk.ehm.sys.api.SysRoleAuthApi;
import com.aijk.ehm.sys.entity.enums.AdminType;
import com.aijk.ehm.sys.model.SysAdminRole;
import com.aijk.ehm.sys.model.SysModule;
import com.aijk.ehm.sys.model.SysRoleAuth;
import com.aijk.ehm.sys.model.qo.AdminRoleQo;
import com.aijk.ehm.sys.model.qo.ModuleQo;
import com.aijk.ehm.sys.model.qo.RoleAuthQo;
import com.aijk.web.admin.shiro.ShiroDbRealm;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>  </p>
 * User: chendw
 * Date: 2016/11/23 13:04
 */
@Service
public class ModuleService {

    @Autowired
    private ModuleApi moduleApi;
    @Autowired
    private SysAdminRoleApi adminRoleApi;
    @Autowired
    private SysRoleAuthApi roleAuthApi;

    /**
     * 获取用户拥有权限的模块列表
     *
     * @param user
     * @return
     */
    public List<SysModule> getUserModuleList(ShiroDbRealm.ShiroUser user) {
        return getUserModuleList(user, null);
    }

    public List<SysModule> getUserModuleList(ShiroDbRealm.ShiroUser user, Integer category) {
        List<SysModule> list;
        ModuleQo qo = new ModuleQo();
        qo.setSysFlag(user.getSysFlag());
        if (category != null) {
            qo.setCategory(category);
        }
        qo.setSortway("orderNo");
        if (user.getType() == AdminType.SUPER_ADMIN.getValue()) { // 超级管理员
            list = moduleApi.get(qo);
        } else {
            AdminRoleQo adminRoleQo = new AdminRoleQo();
            adminRoleQo.setAdminId(user.getUserId());
            List<SysAdminRole> adminRoleList = adminRoleApi.getList(adminRoleQo);
            //这里可能为空，要加判断
            RoleAuthQo roleAuthQo = new RoleAuthQo();
            List<Integer> roleIds = Collections3.extractToList(adminRoleList, "roleId");
            if(roleIds!=null&&roleIds.size()>0)
            roleAuthQo.setRidList(roleIds);
            List<SysRoleAuth> roleAuthList = roleAuthApi.getList(roleAuthQo);
            qo.setIds(Collections3.extractToList(roleAuthList, "mid"));
            list = moduleApi.get(qo);
        }
        return list;
    }

    public List<SysModule> findAll(ModuleQo qo) {
        return moduleApi.get(qo);
    }
}
